//
//  BCNetworkClient.m
//  BCManagerVersion
//
//  Created by ZhaoXiang on 2/22/14.
//  Copyright (c) 2014 Jungle. All rights reserved.
//

#import "BCNetworkClient.h"
#import "../../AFNetworking/AFNetworking/AFNetworking.h"

#define URL_DOMAIN @"http://api.limifortune.com"

@implementation BCNetworkClient

+ (void)loginWithPassport:(NSString *)passport
                 password:(NSString *)password
                     type:(NSString *)type
                  success:(void (^)(int uid, NSString *auth))success
                  failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/passport/login";
	NSDictionary *parameters = @{ @"passport" : passport,
		                          @"password" : password,
		                          @"type" : type };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success([responseObject[@"uid"] intValue], responseObject[@"auth"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)securityCodeSuccess:(void (^)(NSString *securityId, NSString *securityImageUrl))success
                    failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/tools/securitycode";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"securityId"], responseObject[@"securityImage"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)registerWithPassport:(NSString *)passport
                    password:(NSString *)password
                        type:(NSString *)type
                    username:(NSString *)username
                       email:(NSString *)email
                  securityId:(NSString *)securityId
                securityCode:(NSString *)securityCode
                     success:(void (^)(int uid, NSString *auth))success
                     failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/passport/register";
	NSDictionary *parameters = @{ @"passport" : passport,
		                          @"password" : password,
		                          @"type" : type,
		                          @"username" : username,
		                          @"email" : email,
		                          @"securityId" : securityId,
		                          @"securityCode" : securityCode, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success([responseObject[@"uid"] intValue], responseObject[@"auth"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)banksSuccess:(void (^)(NSArray *banks))success
             failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/tools/banks";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 NSMutableArray *banks = [NSMutableArray array];
                 [(NSDictionary *)responseObject enumerateKeysAndObjectsUsingBlock : ^(id key, id obj, BOOL *stop) {
                     if (![key isEqualToString:@"status"]) {
                         [banks addObject:obj];
                     }
                 }];
                 
                 
                 success(banks);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)worksSuccess:(void (^)(NSArray *works))success
             failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/tools/works";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 NSMutableArray *works = [NSMutableArray array];
                 [(NSDictionary *)responseObject enumerateKeysAndObjectsUsingBlock : ^(id key, id obj, BOOL *stop) {
                     if (![key isEqualToString:@"status"]) {
                         [works addObject:obj];
                     }
                 }];
                 
                 
                 success(works);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)productIconsSuccess:(void(^)(NSArray* icons))success
                    failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/tools/producticons";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 NSMutableArray *producticons = [NSMutableArray array];
                 [(NSDictionary *)responseObject enumerateKeysAndObjectsUsingBlock : ^(id key, id obj, BOOL *stop) {
                     if (![key isEqualToString:@"status"]) {
                         [producticons addObject:obj];
                     }
                 }];
                 
                 
                 success(producticons);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)protocolRegisterSuccess:(void(^)(NSString* type, NSString* protocol))success
                        failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/tools/protocol/register";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 
                 
                 success(responseObject[@"type"], responseObject[@"protocol"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)forgetPasswordSuccess:(void(^)(NSString* message))success
                      failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/message/forget_passwd";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 
                 
                 success(responseObject[@"message"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)aboutUsSuccess:(void(^)(NSString* message))success
               failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/message/about_us";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 
                 
                 success(responseObject[@"message"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)searchWithKey:(NSString *)key
                 auth:(NSString *)auth
                 city:(NSString *)city //?? id or name
                 work:(NSString *)work //?? id or name
                 bank:(NSString *)bank //?? id or name
                order:(NSString *)order //?? what values?
                 desc:(NSString *)desc //?? what values?
                start:(int)start
                limit:(int)limit
              success:(void (^)(NSArray *results))success
              failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/search/query";
	NSDictionary *parameters = @{ @"key" : key,
		                          @"auth" : auth,
		                          @"city" : city,
		                          @"work" : work,
		                          @"bank" : bank,
		                          @"orders" : order,
		                          @"desc" : desc,
		                          @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 NSMutableArray *works = [NSMutableArray array];
                 [(NSDictionary *)responseObject enumerateKeysAndObjectsUsingBlock : ^(id key, id obj, BOOL *stop) {
                     if (![key isEqualToString:@"status"]) {
                         [works addObject:obj];
                     }
                 }];
                 
                 
                 success(works);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)userInfo:(NSNumber *)uid
            type:(NSString *)type //detail
         success:(void (^)(NSDictionary *info))success
         failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/info";
	NSDictionary *parameters = @{ @"uid" : uid,
		                          @"type" : type, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)adsInfo:(NSNumber *)uid
           type:(NSString *)type
   reqFriendUid:(NSNumber *)reqFriendUid
        success:(void (^)(NSDictionary *info))success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/adsinfo";
	NSDictionary *parameters = @{ @"uid" : uid,
		                          @"type" : type,
                                  @"reqFriendUid" : reqFriendUid
                                  };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)product:(NSNumber *)uid
           type:(NSString *)type
          start:(int)start
          limit:(int)limit
        success:(void (^)(NSNumber *total, NSArray *products))success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/product";
	NSDictionary *parameters = @{ @"uid" : uid,
		                          @"type" : type,
		                          @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"products"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)comment:(NSNumber *)uid
           type:(NSString *)type
          start:(int)start
          limit:(int)limit
        success:(void (^)(NSNumber *total, NSNumber *score, NSArray *comments))success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/comment";
	NSDictionary *parameters = @{ @"uid" : uid,
		                          @"type" : type,
		                          @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"score"], responseObject[@"comments"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)friends:(NSString *)auth
          touid:(NSNumber *)touid
           type:(NSString *)type
        success:(void (^)())success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/friends";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"touid" : touid,
		                          @"type" : type, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)comment:(NSString *)auth
          touid:(NSNumber *)touid
           type:(NSString *)type //add
          score:(NSNumber *)score
        message:(NSString *)message
        success:(void (^)())success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/comment";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"touid" : touid,
		                          @"type" : type,
		                          @"score" : score,
		                          @"message" : message };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)resetpwdWithCurpwd:(NSString *)curpwd
                    newpwd:(NSString *)newpwd
                      auth:(NSString *)auth
                   success:(void (^)())success
                   failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/passport/resetpwd";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"curpwd" : curpwd,
		                          @"newpwd" : newpwd };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)bindPhoneId:(NSString *)phoneId
               auth:(NSString *)auth
            success:(void (^)())success
            failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/passport/bind";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"iphoneId" : phoneId, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)sendMessage:(NSString *)auth
                 to:(NSNumber *)uid
            message:(NSString *)message
            success:(void (^)())success
            failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/forward";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"to" : uid,
		                          @"message" : message, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)bankAds:(int)start
          limit:(int)limit
        success:(void (^)(NSNumber *total, NSString *type, NSArray *ads))success
        failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/bank";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)zixunAds:(int)start
           limit:(int)limit
         success:(void (^)(NSNumber *total, NSString *type, NSArray *ads))success
         failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/zixun";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)zixunbanner:(int)start
              limit:(int)limit
            success:(void (^)(NSNumber *total, NSString *type, NSArray *ads))success
            failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/zixunbanner";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)merchant:(int)start
           limit:(int)limit
         success:(void (^)(NSNumber *total, NSString *type, NSArray *ads))success
         failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/merchant";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)merchantbanner:(int)start
                 limit:(int)limit
               success:(void (^)(NSNumber *total, NSString *type, NSArray *ads))success
               failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/message/merchantbanner";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)sysads:(int)start
         limit:(int)limit
       success:(void(^)(NSNumber* total, NSString* type, NSArray* ads))success
       failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/message/sysads";
	NSDictionary *parameters = @{ @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"type"], responseObject[@"ads"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)editUsername:(NSString *)type //edit
                auth:(NSString *)auth
            username:(NSString *)username
                city:(NSString *)city
             success:(void (^)())success
             failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/info";
	NSDictionary *parameters = @{ @"type" : type,
		                          @"auth" : auth,
		                          @"username" : username,
                                  @"city" : city};
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}


+ (void)editAdsUserInfo:(NSString *)type
                   auth:(NSString *)auth
                    tel:(NSString *)tel
               bankMain:(NSString *)bankMain
               bankSub1:(NSString *)bankSub1
               bankSub2:(NSString *)bankSub2
                 gender:(NSNumber *)gender
                    age:(NSString *)age
               workTime:(NSNumber *)workTime
               workType:(NSString *)workType
                   city:(NSString *)city
                address:(NSString*)address
                   name:(NSString*)name
                success:(void (^)())success
                failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/adsinfo";
	NSDictionary *parameters = @{ @"type" : type,
		                          @"auth" : auth,
		                          @"tel" : tel,
		                          @"bankMain" : bankMain,
		                          @"bankSub1" : bankSub1,
		                          @"bankSub2" : bankSub2,
		                          @"gender" : gender,
		                          @"age" : age,
		                          @"workTime" : workTime,
		                          @"workType" : workType,
		                          @"city" : city,
                                  @"address" : address,
                                  @"name" : name};
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"URL: %@", operation.request.URL);
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

static NSString * const kAFCharactersToBeEscapedInQueryString = @":/?&=;+!@#$()',*";

static NSString * AFPercentEscapedQueryStringKeyFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
    static NSString * const kAFCharactersToLeaveUnescapedInQueryStringPairKey = @"[].";
    
	return (__bridge_transfer  NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescapedInQueryStringPairKey, (__bridge CFStringRef)kAFCharactersToBeEscapedInQueryString, CFStringConvertNSStringEncodingToEncoding(encoding));
}


+ (void)uploadAvatar:(NSString *)auth
           imageData:(NSData *)imageData
             success:(void(^)(NSString* avatarUrl, NSString* tAvatar))success
             failure:(void (^)(NSError *error))failure {
    
    NSString* URLString = [NSString stringWithFormat:URL_DOMAIN "/api/user/avatar?auth=%@", AFPercentEscapedQueryStringKeyFromStringWithEncoding(auth, NSUTF8StringEncoding)];
    
	[[AFHTTPRequestOperationManager manager] POST:URLString
	                                   parameters:nil
	                    constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
                            [formData appendPartWithFileData:imageData
                                                        name:@"card"
                                                    fileName:@"image.jpg"
                                                    mimeType:@"image/jpeg"];
                        } success: ^(AFHTTPRequestOperation *operation, id responseObject) {
                            NSLog(@"JSON: %@", responseObject);
                            if ([responseObject[@"status"] intValue] == STATUS_OK) {
                                success(responseObject[@"avatar"], responseObject[@"tAvatar"]);
                            }
                            else {
                                failure([NSError errorWithDomain:@"api"
                                                            code:[responseObject[@"status"] intValue]
                                                        userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
                            }
                        } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
                            NSLog(@"Error: %@", error);
                            failure(error);
                        }];
}

+ (void)userFiends:(NSString *)auth
              type:(NSString *)type //list
             start:(int)start
             limit:(int)limit
           success:(void (^)(NSNumber *total, NSArray *friends))success
           failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/friends";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"type" : type,
		                          @"start" : @(start),
		                          @"limit" : @(limit) };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"friends"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)nickname:(NSString *)auth
           touid:(NSNumber *)touid
            type:(NSString *)type //setNickname
        nickname:(NSString *)nickname
         success:(void (^)())success
         failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/friends";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"type" : type,
		                          @"touid" : touid,
		                          @"nickname" : nickname };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"total"], responseObject[@"friends"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)publishProduct:(NSString *)auth
                  type:(NSString *)type //add
                 title:(NSString*)title
               message:(NSString *)message
                 image:(NSString *)image //产品信息图片(由媒体文件API接口上传得到的URL)
               success:(void (^)(NSNumber* productId))success
               failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/product";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"type" : type,
                                  @"title" : title,
		                          @"message" : message,
		                          @"image" : image };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"id"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)editProduct:(NSString *)auth
               type:(NSString *)type //edit
          productId:(NSNumber *)productId
              title:(NSString*)title
            message:(NSString *)message
              image:(NSString *)image //产品信息图片(由媒体文件API接口上传得到的URL)
            success:(void (^)())success
            failure:(void (^)(NSError *error))failure;
{
	NSString *URLString = URL_DOMAIN "/api/user/product";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"type" : type,
                                  @"title" : title,
		                          @"message" : message,
		                          @"image" : image,
		                          @"id" : productId, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)removeProduct:(NSString *)auth
                 type:(NSString *)type //remove
            productId:(NSNumber *)productId
              success:(void (^)())success
              failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/user/product";
	NSDictionary *parameters = @{ @"auth" : auth,
		                          @"type" : type,
		                          @"id" : productId, };
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success();
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)uploadChatLog:(NSArray *)logs
              success:(void (^)())success
              failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/chat/logs";
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
	[manager POST:URLString parameters:(id)logs
	      success: ^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"JSON: %@", responseObject);
              if ([responseObject[@"status"] intValue] == STATUS_OK) {
                  success();
              }
              else {
                  failure([NSError errorWithDomain:@"api"
                                              code:[responseObject[@"status"] intValue]
                                          userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
              }
          } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              failure(error);
          }];
}

+ (void)uploadMedia:(NSString *)auth
            fileURL:(NSURL *)fileURL
               name:(NSString *)name
            success:(void (^)())success
            failure:(void (^)(NSError *error))failure {
	NSString *URLString = URL_DOMAIN "/api/tools/uploadmedia";
	NSDictionary *parameters = nil;
    
	[[AFHTTPRequestOperationManager manager] POST:URLString
	                                   parameters:parameters
	                    constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
                            NSError *error = nil;
                            [formData appendPartWithFileURL:fileURL
                                                       name:name
                                                      error:&error];
                            if (error) {
                                NSLog(@"Error: %@", error);
                                failure(error);
                            }
                        } success: ^(AFHTTPRequestOperation *operation, id responseObject) {
                            NSLog(@"JSON: %@", responseObject);
                            if ([responseObject[@"status"] intValue] == STATUS_OK) {
                                success();
                            }
                            else {
                                failure([NSError errorWithDomain:@"api"
                                                            code:[responseObject[@"status"] intValue]
                                                        userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
                            }
                        } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
                            NSLog(@"Error: %@", error);
                            failure(error);
                        }];
}


+ (void)merchantAdsMoreSuccess:(void(^)(NSString* message))success
                       failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/message/merchant_ads_more";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"message"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

+ (void)hotWordsSuccess:(void(^)(NSDictionary* hot, NSDictionary* prompt))success
                failure:(void(^)(NSError* error))failure
{
    NSString *URLString = URL_DOMAIN "/api/search/words";
	NSDictionary *parameters = nil;
    
	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:URLString parameters:parameters
	     success: ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
             if ([responseObject[@"status"] intValue] == STATUS_OK) {
                 success(responseObject[@"hot"], responseObject[@"prompt"]);
             }
             else {
                 failure([NSError errorWithDomain:@"api"
                                             code:[responseObject[@"status"] intValue]
                                         userInfo:@{ NSLocalizedDescriptionKey: responseObject[@"message"] }]);
             }
         } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             failure(error);
         }];
}

@end


@implementation NSError (BCNetworkClient)
- (NSString*)message
{
    if ([self.domain isEqualToString:@"api"]) {
        return self.localizedDescription;
    } else if ([self.domain isEqualToString:AFNetworkingErrorDomain]
               || [self.domain isEqualToString:NSURLErrorDomain]) {
        return @"连接失败";
    }
    return self.localizedDescription;
}
@end

